<?php


namespace catchAdmin\sms\controller;


use app\common\lib\ZhyxBeanstalkd;
use catchAdmin\permissions\model\Department;
use catchAdmin\sms\request\CreateRequest;
use catcher\base\CatchController;
use catcher\base\CatchModel;
use catcher\base\CatchRequest;
use catcher\CatchResponse;
use catcher\exceptions\FailedException;
use catcher\exceptions\ValidateFailedException;
use think\facade\Log;
use think\Response;

class Sms extends CatchController
{
    /**
     *
     * @time 2022/10/08 11:22
     * @return Response
     */
    public function index(): Response
    {
        return CatchResponse::success($this->Sms->getList());
    }


    public function save(CreateRequest $request)
    {

        try {
            $sendDeptInfo = (new Department())->findBy($request->user()["department_id"]);
            $postData = $request->param();
            $postData["deleted_at"] = 0;
            $postData["send_person"] = $request->user()["username"];
            $postData["send_dept_name"] = $sendDeptInfo["department_name"] ?? "";
            $postData["send_dept_id"] = $request->user()["department_id"];
            $postData["tubeName"] = "SendSms";
            $userInfo = $request->user();
            $postData["userInfo"] = [
                "id" => $userInfo["id"],
                "username" => $userInfo["username"],
                "doctor_code" => $userInfo["doctor_code"],
                "dept_name" => $sendDeptInfo["department_name"] ?? "",
            ];
            $result = ZhyxBeanstalkd::getInstance()->beansTalk->useTube("sendSms")->put(json_encode($postData, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE));
            if ($result->getId()) {
                return CatchResponse::success(["taskId" => $result->getId()]);
            }
        } catch (\Exception $exception) {
            Log::error("批量发送短信任务投递失败:" . json_encode($postData, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE) . ",失败原因:" . $exception->getMessage());
            throw new FailedException("任务投递失败:" . $exception->getMessage());
        }
        return CatchResponse::fail("任务投递失败");
    }

    public function getBalance(CatchRequest $request): \think\response\Json
    {
        //$result = \app\common\lib\Sms::getInstance()->getBalace();
        $result = \app\common\lib\Sms::getInstance()->getBalance();
        return CatchResponse::success([
            "payinfo" => $result["payinfo"] ?? "",
            "overage" => $result["overage"] ?? 0,
            "sendTotal" => $result["sendTotal"] ?? 0
        ]);
    }

    public function checkSmsContent(CatchRequest $request): \think\response\Json
    {
        $content = $request->param("content", "", "string");

        if (empty(trim($content))) {
            throw new ValidateFailedException("短信内容不能为空");
        }
        $result = \app\common\lib\Sms::getInstance()->checkMessageContent($content);
        if ($result["returnstatus"] === "Success") {
            //return showJson(ApiErrDesc::OK[0], "检测成功");
            return CatchResponse::success("检测成功");
        }
        return CatchResponse::fail("检测失败");
    }


}